package com.wzw.conference.entity;

import static com.wzw.conference.constant.TimePoint.LUNCH_START_TIME;
import static com.wzw.conference.constant.TimePoint.NETWORKING_END_START_TIME;
import static com.wzw.conference.constant.TimePoint.NETWORKING_START_TIME;

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 * @author wzw
 * @date 2023/3/23
 */
@Data
@NoArgsConstructor
public class Session {

  private SessionName name;
  private int startTime;
  private int fillTime;
  private int endTime;
  private List<Talk> talks = new ArrayList<>();


  public Session(SessionName name, int startTime, int fillTime, int endTime) {
    this.name = name;
    this.startTime = startTime;
    this.fillTime = fillTime;
    this.endTime = endTime;
  }


  public boolean canAddTalk(Talk talk) {
    int newFillTime = fillTime + talk.getDuration();
    if (name.equals(SessionName.MORNING_SESSION)) {
      return newFillTime <= LUNCH_START_TIME
          && !talks.contains(talk);
    }
    return newFillTime <= NETWORKING_END_START_TIME && !talks.contains(talk);
  }


  public void addTalk(Talk talk) {
    String network = "NetWork Event";
    if (network.equals(talk.getTitle()) && fillTime < NETWORKING_START_TIME) {
      talk.setSchedule(formatTime(NETWORKING_START_TIME));
      talks.add(talk);
      return;
    }

    fillTime += talk.getDuration();
    talk.setSchedule(formatTime(fillTime - talk.getDuration()));
    talks.add(talk);
  }


  public boolean addNetWorkEvent(int duration) {
    if (name.equals(SessionName.AFTERNOON_SESSION)
        && fillTime <= NETWORKING_END_START_TIME) {
      addTalk(new Talk("NetWork Event", duration));
      return true;
    }
    return false;
  }

  protected String formatTime(int timeInMinutes) {
    int noonTime = 12;
    int hour = timeInMinutes / 60;
    int minute = timeInMinutes % 60;
    String amPm = hour < 12 ? "AM" : "PM";
    if (hour > noonTime) {
      hour -= 12;
    }
    return String.format("%d:%02d%s", hour, minute, amPm);
  }


}
